package com.itheima.info.manager.controller;

import com.itheima.info.manager.domain.Student;
import com.itheima.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {

    public Scanner sc = new Scanner(System.in);
    private StudentService s = new StudentService();

    public final void start() {
        String choice;

        while (true) {
            System.out.println("--------欢迎来到<学生>信息管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.修改学生");
            System.out.println("3.删除学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出系统");
            choice = sc.next();

            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    updateStudent();
                    break;
                case "3":
                    deleteStudent();
                    break;
                case "4":
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("退出成功");
                    return;
                default:
                    System.out.println("您的输入有误,请重新输入");

            }
        }
    }

    //客服增加
    public final void addStudent() {
        //键盘录入学生信息
        System.out.println("请输入学生学号:");
        String id = inputId(false);

        Student stu = sealStudent(id);

        //调用service方法
        boolean result = s.addStudent(stu);

        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //删除方法
    private final void deleteStudent() {
        //先判断系统中是否有数据
        Student[] stus = s.findAllStudent();
        if (stus == null) {
            System.out.println("查无信息,请输入后在删除");
        }
        System.out.println("请输入需要删除的学生学号");
        String delId = sc.next();
        boolean result = s.deleteStudent(delId);
        if (result) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    //创建客服中的修改学生信息
    public void updateStudent() {
        Student[] stus = s.findAllStudent();
        if (stus == null) {
            System.out.println("暂时没有数据,请添加后重试");
            return;
        }
        //接收学生数的学号
        System.out.println("请输入要修改的学生学号:");
        String updateId = inputId(true);

        //调用sealStudent()方法
        Student stu = sealStudent(updateId);

        s.updateStudent(updateId, stu);
        System.out.println("修改成功");
    }

    //客服查询
    public final void findAllStudent() {
        Student[] stus = s.findAllStudent();
        if (stus != null) {
            System.out.println("学号\t姓名\t年龄\t生日");
            for (int i = 0; i < stus.length; i++) {
                Student stu = stus[i];
                if (stu != null) {

                    System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
                }
            }
        } else {
            System.out.println("查无信息,请添加");
        }
    }


    //封装一个方法,校验用户录入的学号是否存在
    //增加学生,想要不存在(false). 修改学生想要存在(true)
    public final String inputId(boolean flag) {

        String sSid;

        while(true){
            //用户录入学生学号
            sSid = sc.next();

            //调用isExists()方法,获取返回的boolean值
            boolean result = s.isExists(sSid);

            //判断用户录入的学号是否存在
            if(flag != result){
                break;
            }else{
                System.out.println("您输入的学号有误!请重新输入: ");
            }
        }
        return sSid;
    }

    //抽象一个方法,封装学生信息,获取学生对象
    public abstract Student sealStudent(String sSid);
}
